//
//  NSObject+RxSwift.swift
//  Pods
//
//  Created by Rdxer on 2017/8/15.
//
//

import UIKit
import RxSwift


public extension ObservableType {
    
    // 使用此方法,不会监听到 error 和 onCompleted
    public func subscribeOnNext<Object: NSObject>(
        with object: Object,
        onNext: ((Object, Element) -> Void)? = nil,
        onError: ((Object, Swift.Error) -> Void)? = nil,
        onCompleted: ((Object) -> Void)? = nil,
        onDisposed: ((Object) -> Void)? = nil
    ) {
        subscribe(
            onNext: { [weak object] in
                guard let object = object else { return }
                onNext?(object,$0)
            },
            onError: { [weak object] in
                guard let object = object else { return }
                onError?(object, $0)
            },
            onCompleted: { [weak object] in
                guard let object = object else { return }
                onCompleted?(object)
            },
            onDisposed: { [weak object] in
                guard let object = object else { return }
                onDisposed?(object)
            }
        ).disposed(by: object)
    }
    // 使用此方法,不会监听到 error 和 onCompleted
    public func subscribeOnNext (
        onNext: ((  Element) -> Void)? ,
        onError: (( Swift.Error) -> Void)? = nil,
        onCompleted: (( ) -> Void)? = nil,
        onDisposed: (( ) -> Void)? = nil
    ) -> Disposable {
        subscribe(
            onNext: {
                onNext?( $0)
            },
            onError: {
                 
                onError?(   $0)
            },
            onCompleted: {
                onCompleted?( )
            },
            onDisposed: {
                onDisposed?( )
            }
        )
    }
}
 
